package crons

import "strings"

/**
 * cron 任务执行时间间隔
 */
type TimeInterval string

/**
 * 几个 cron 的预定义时间规则
 */
const (
	YEARLY  TimeInterval = "@yearly"  // 按年执行
	MONTHLY TimeInterval = "@monthly" // 按月执行
	WEEKLY  TimeInterval = "@weekly"  // 按周执行
	DAILY   TimeInterval = "@daily"   // 按日执行
	HOURLY  TimeInterval = "@hourly"  // 按时执行
)

/**
 * 时间间隔转换成字符串
 */
func (cti TimeInterval) String() string {
	return string(cti)
}

/**
 * 时间间隔转换成字符串
 */
func (cti TimeInterval) DataFormat(separator ...string) string {
	var ps []string = make([]string, 0)
	switch cti {
	case YEARLY:
		ps = append(ps, "2006")
	case MONTHLY:
		ps = append(ps, "2006", "01")
	case HOURLY:
		ps = append(ps, "2006", "01", "02", "15")
	case DAILY:
		fallthrough
	default:
		ps = append(ps, "2006", "01", "02")
	}
	if len(separator) > 0 {
		return strings.Join(ps, separator[0])
	}
	return strings.Join(ps, "")
}
